import React from 'react'

import RealTimeCodeEditor from '@/components/amis/RealTimeCodeEditor/RealTimeCodeEditor'

import { ApiObject } from 'amis-core/lib/types'
import rsqlUtils from '@/core/utils/rsql/rsqlUtils'
import coreConstants from '@/core/constants/coreConstants'
import { RsqlDataOperator } from '@/core/utils/rsql/types/RsqlDataOperator'
import { RsqlDataOperatorEnum } from '@/core/utils/rsql/types/RsqlOperatorEnum'
import defaultCrudTableUtils from '@/components/DefaultCrudTable/defaultCrudTableUtils'
import iamAppIdColumnUtils from '@/components/account/iamAppIdColumnUtils'
import iamAppIdSelectUtils from '@/components/account/iamAppIdSelectUtils'
import iamAccountEventRecordTypeColumnUtils from '@/components/account/iamAccountEventRecordTypeColumnUtils'
import iamAccountEventRecordTypeSelectUtils from '@/components/account/iamAccountEventRecordTypeSelectUtils'
import { DateRangeControlSchema } from 'amis/lib/renderers/Form/InputDateRange'
import accountAdminApiConfig from '@/core/account/generated/apiconfig/accountAdminApiConfig'

const IamAccountEventRecordList = () => {
	const entityName = '账户操作日志'
	const isModifyModeExpression = '${isEditMode}'
	const modelApiConfig = accountAdminApiConfig.iamAccountEventRecord
	let schema = defaultCrudTableUtils.getSchema({
		entityTitle: `${entityName}`,
		searchServerApi: {
			apiConfig: modelApiConfig.search,
			requestAdaptor: (api: ApiObject, context: any): ApiObject | Promise<ApiObject> => {
				const operators: RsqlDataOperator[] = [
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'id',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'iamAppId',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'accountId',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'eventType',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.GREATER_OR_EQUAL,
						propertyName: 'createTime',
						paramPropertyName: 'createTimeBegin',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.LESS_OR_EQUAL,
						propertyName: 'createTime',
						paramPropertyName: 'createTimeEnd',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'deleteFlag',
					} as RsqlDataOperator,
				]
				context[coreConstants.deleteFlagFieldName] = coreConstants.notDeleteFlag
				api = rsqlUtils.addRsqlFilter(api, context, coreConstants.api.queryFilterName, operators)
				return api
			},
		},
		addServerApi: undefined,
		deleteServerApi: undefined,
		detailServerApi: {
			apiConfig: modelApiConfig.get,
			data: {
				id: '${id}',
			},
		},
		editServerApi: undefined,
		debug: false,
		formItems: [],
		columns: [
			{
				name: 'id',
				label: 'ID',
				searchable: true,
			},
			{
				...iamAppIdColumnUtils.getSchema(),
				searchable: iamAppIdSelectUtils.getSchema(),
			},
			{
				name: 'accountId',
				label: '账户ID',
				searchable: true,
			},
			{
				...iamAccountEventRecordTypeColumnUtils.getSchema(),
				searchable: iamAccountEventRecordTypeSelectUtils.getSchema(),
			},
			{
				name: 'eventParams',
				label: '事件参数-json格式',
			},
			{
				type: 'date',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				searchable: {
					type: 'input-datetime-range',
					name: 'createTimeBegin',
					extraName: 'createTimeEnd',
					label: '创建时间',
					valueFormat: 'x',
					displayFormat: 'YYYY-MM-DD HH:mm:ss',
					shortcuts: 'today,yesterday,7daysago,prevweek,thismonth,prevmonth,thisyear,lastyear',
					timeFormat: 'HH:mm:ss',
				} as DateRangeControlSchema,
			},
			{
				type: 'date',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
		],
	})
	return <RealTimeCodeEditor code={schema} />
}

export default IamAccountEventRecordList
